﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace DAO
{
    public class EnrollInformationDAO
    {
        #region GetListEnrollInformationsRSS
            public static XmlElement GetListEnrollInformationsRSS()
            {
                MemoryStream stm = new MemoryStream();
                stm = CreateRSSListEnroll();

                // seek to start position
                stm.Seek(0, SeekOrigin.Begin);

                XmlTextReader xr = new XmlTextReader(stm);
                XmlDocument doc = new XmlDocument();
                doc.Load(xr);

                return doc.DocumentElement;
            }

            public static MemoryStream CreateRSSListEnroll()
            {
                MemoryStream stm = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(stm, Encoding.UTF8);

                // start writing the rss tags
                writer.WriteStartDocument();
                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteStartElement("channel");
                writer.WriteElementString("title", "ListMajor - Rongcon.com");
                writer.WriteElementString("link", "http://localhost:8080/itemService/ListEnroll.rss");
                writer.WriteElementString("description", "I'm RongCon");
                writer.WriteElementString("copyright", "Copyright by RongCon!");

                List<ENROLLINFORMATION> lstEnrolls = GetListEnrollInformations();

                if (lstEnrolls != null)
                {
                    for (int i = 0; i < lstEnrolls.Count; i++)
                    {
                        EMPLOYERACCOUNT employer = GetEmployerWithID(Int32.Parse(lstEnrolls[i].UserID.ToString()));
                        ENROLLMAJOR major = GetMajorWithID(Int32.Parse(lstEnrolls[i].MajorID.ToString()));

                        writer.WriteStartElement("item");
                        writer.WriteElementString("ID", lstEnrolls[i].ID.ToString());
                        writer.WriteElementString("EnrName", lstEnrolls[i].EnrName);
                        writer.WriteElementString("EnrDescription", lstEnrolls[i].EnrDescription);
                        writer.WriteElementString("EnrRequire", lstEnrolls[i].EnrRequire);
                        writer.WriteElementString("CompanyName", employer.CompanyName);
                        writer.WriteElementString("CompanyDescription", employer.CompanyDescription);
                        writer.WriteElementString("CompanyLink", employer.Link);
                        writer.WriteElementString("EnrLevel", lstEnrolls[i].EnrLevel);
                        writer.WriteElementString("EnrAge", lstEnrolls[i].EnrAge.ToString());
                        writer.WriteElementString("EnrSex", lstEnrolls[i].EnrSex);
                        writer.WriteElementString("Diploma", lstEnrolls[i].Diploma);
                        writer.WriteElementString("EnglishLevel", lstEnrolls[i].EnglishLevel);
                        writer.WriteElementString("ExperienceNumber", lstEnrolls[i].ExperienceNumber.ToString());
                        writer.WriteElementString("Quantity", lstEnrolls[i].Quantity.ToString());
                        writer.WriteElementString("Salary", lstEnrolls[i].Salary);
                        writer.WriteElementString("MajorName", major.MajorName);
                        writer.WriteElementString("IsShow", lstEnrolls[i].IsShow.ToString());
                        writer.WriteElementString("CreatedDate", lstEnrolls[i].CreatedDate.ToString());
                        writer.WriteElementString("EndDate", lstEnrolls[i].EndDate.ToString());
                        writer.WriteElementString("Area", lstEnrolls[i].Area);
                        writer.WriteElementString("Lat", lstEnrolls[i].Lat);
                        writer.WriteElementString("Lng", lstEnrolls[i].Lng);
                        writer.WriteElementString("LeaderName", lstEnrolls[i].LeaderName);
                        writer.WriteElementString("LeaderPhone", lstEnrolls[i].LeaderPhone);
                        writer.WriteElementString("LeaderEmail", lstEnrolls[i].LeaderEmail);
                        writer.WriteEndElement();
                    }
                }

                // Close all tags
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();

                return stm;
            }

            public static List<ENROLLINFORMATION> GetListEnrollInformations()
            {
                FindJobDBDataContext db = new FindJobDBDataContext();

                List<ENROLLINFORMATION> result = new List<ENROLLINFORMATION>();

                var query = from n in db.ENROLLINFORMATIONs
                            select n;

                foreach (ENROLLINFORMATION r in query)
                    result.Add(r);

                return result;
            }

        #endregion

        #region AddEnrollInformation
            public static void AddEnrollInformation(string EnrName, string mota, string yeucau, string luong, string userid, string age, string gt, string eng, string exp, string major, string lname, string lphone, string email)
            {
                FindJobDBDataContext db = new FindJobDBDataContext();
                ENROLLINFORMATION inf = new ENROLLINFORMATION();
                inf.EnrName = EnrName;
                inf.EnrDescription = mota;
                inf.EnrRequire = yeucau;
                inf.Salary = luong;
                inf.UserID = int.Parse(userid);
                inf.EnrAge = int.Parse(age);
                inf.EnrSex = age;
                inf.EnglishLevel = eng;
                inf.ExperienceNumber = int.Parse(exp);
                inf.MajorID = int.Parse(major);
                inf.LeaderName = lname;
                inf.LeaderPhone = lphone;
                inf.LeaderEmail = email;
                db.ENROLLINFORMATIONs.InsertOnSubmit(inf);
                db.SubmitChanges();
            }
        #endregion

        #region GetItemEnrollInformation
            public static XmlElement GetItemEnrollInformation(string id)
            {
                MemoryStream stm = new MemoryStream();
                stm = CreateDetailsEnrollXML(id);

                // seek to start position
                stm.Seek(0, SeekOrigin.Begin);

                XmlTextReader xr = new XmlTextReader(stm);
                XmlDocument doc = new XmlDocument();
                doc.Load(xr);

                return doc.DocumentElement;
            }

            public static MemoryStream CreateDetailsEnrollXML(string id)
            {
                MemoryStream stm = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(stm, Encoding.UTF8);

                // start writing the rss tags
                writer.WriteStartDocument();
                writer.WriteStartElement("WebsiteGioiThieuViecLam");
                writer.WriteAttributeString("Action", "GetDetailsEnroll");

                ENROLLINFORMATION lstEnrolls = GetEnrollInformationDetails(id);

                if (lstEnrolls != null)
                {
                    EMPLOYERACCOUNT employer = GetEmployerWithID(Int32.Parse(lstEnrolls.UserID.ToString()));
                    ENROLLMAJOR major = GetMajorWithID(Int32.Parse(lstEnrolls.MajorID.ToString()));

                    writer.WriteElementString("ID", lstEnrolls.ID.ToString());
                    writer.WriteElementString("EnrName", lstEnrolls.EnrName);
                    writer.WriteElementString("EnrDescription", lstEnrolls.EnrDescription);
                    writer.WriteElementString("EnrRequire", lstEnrolls.EnrRequire);
                    writer.WriteElementString("CompanyName", employer.CompanyName);
                    writer.WriteElementString("CompanyDescription", employer.CompanyDescription);
                    writer.WriteElementString("CompanyLink", employer.Link);
                    writer.WriteElementString("EnrLevel", lstEnrolls.EnrLevel);
                    writer.WriteElementString("EnrAge", lstEnrolls.EnrAge.ToString());
                    writer.WriteElementString("EnrSex", lstEnrolls.EnrSex);
                    writer.WriteElementString("Diploma", lstEnrolls.Diploma);
                    writer.WriteElementString("EnglishLevel", lstEnrolls.EnglishLevel);
                    writer.WriteElementString("ExperienceNumber", lstEnrolls.ExperienceNumber.ToString());
                    writer.WriteElementString("Quantity", lstEnrolls.Quantity.ToString());
                    writer.WriteElementString("Salary", lstEnrolls.Salary);
                    writer.WriteElementString("MajorName", major.MajorName);
                    writer.WriteElementString("IsShow", lstEnrolls.IsShow.ToString());
                    writer.WriteElementString("CreatedDate", lstEnrolls.CreatedDate.ToString());
                    writer.WriteElementString("EndDate", lstEnrolls.EndDate.ToString());
                    writer.WriteElementString("Area", lstEnrolls.Area);
                    writer.WriteElementString("Lat", lstEnrolls.Lat);
                    writer.WriteElementString("Lng", lstEnrolls.Lng);
                    writer.WriteElementString("LeaderName", lstEnrolls.LeaderName);
                    writer.WriteElementString("LeaderPhone", lstEnrolls.LeaderPhone);
                    writer.WriteElementString("LeaderEmail", lstEnrolls.LeaderEmail);
                }

                // Close all tags
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();

                return stm;
            }

            public static EMPLOYERACCOUNT GetEmployerWithID(int id)
            {
                FindJobDBDataContext db = new FindJobDBDataContext();
                var query = from n in db.EMPLOYERACCOUNTs
                            where n.ID == id
                            select n;

                foreach (EMPLOYERACCOUNT r in query)
                    return r;

                return null;
            }

            public static ENROLLMAJOR GetMajorWithID(int id)
            {
                FindJobDBDataContext db = new FindJobDBDataContext();
                var query = from n in db.ENROLLMAJORs
                            where n.ID == id
                            select n;

                foreach (ENROLLMAJOR r in query)
                    return r;

                return null;
            }

            public static ENROLLINFORMATION GetEnrollInformationDetails(string id)
            {
                FindJobDBDataContext db = new FindJobDBDataContext();
                int ID = int.Parse(id);

                var query = from n in db.ENROLLINFORMATIONs
                            where n.ID == ID
                            select n;

                foreach (ENROLLINFORMATION r in query)
                    return r;

                return null;
            }

        #endregion
            
        #region Create RSS for every Major
    public static XmlElement GetListEnrollsWithMajor(int majorID)
    {
        MemoryStream stm = new MemoryStream();
        stm = CreateRSSListEnrollWithMajor(majorID);

        // seek to start position
        stm.Seek(0, SeekOrigin.Begin);

        XmlTextReader xr = new XmlTextReader(stm);
        XmlDocument doc = new XmlDocument();
        doc.Load(xr);

        return doc.DocumentElement;
    }

    public static MemoryStream CreateRSSListEnrollWithMajor(int majorID)
    {
        MemoryStream stm = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(stm, Encoding.UTF8);

        // start writing the rss tags
        writer.WriteStartDocument();
        writer.WriteStartElement("rss");
        writer.WriteAttributeString("version", "2.0");
        writer.WriteStartElement("channel");
        writer.WriteElementString("title", "ListMajor - Rongcon.com");
        writer.WriteElementString("link", "http://localhost:8080/itemService/ListEnroll.rss");
        writer.WriteElementString("description", "I'm RongCon");
        writer.WriteElementString("copyright", "Copyright by RongCon!");

        List<ENROLLINFORMATION> lstEnrolls = GetListEnrollsWithMajorID(majorID);

        if (lstEnrolls != null)
        {
            for (int i = 0; i < lstEnrolls.Count; i++)
            {
                EMPLOYERACCOUNT employer = GetEmployerWithID(Int32.Parse(lstEnrolls[i].UserID.ToString()));
                ENROLLMAJOR major = GetMajorWithID(Int32.Parse(lstEnrolls[i].MajorID.ToString()));

                writer.WriteStartElement("item");
                writer.WriteElementString("ID", lstEnrolls[i].ID.ToString());
                writer.WriteElementString("EnrName", lstEnrolls[i].EnrName);
                writer.WriteElementString("EnrDescription", lstEnrolls[i].EnrDescription);
                writer.WriteElementString("EnrRequire", lstEnrolls[i].EnrRequire);
                writer.WriteElementString("CompanyName", employer.CompanyName);
                writer.WriteElementString("CompanyDescription", employer.CompanyDescription);
                writer.WriteElementString("CompanyLink", employer.Link);
                writer.WriteElementString("EnrLevel", lstEnrolls[i].EnrLevel);
                writer.WriteElementString("EnrAge", lstEnrolls[i].EnrAge.ToString());
                writer.WriteElementString("EnrSex", lstEnrolls[i].EnrSex);
                writer.WriteElementString("Diploma", lstEnrolls[i].Diploma);
                writer.WriteElementString("EnglishLevel", lstEnrolls[i].EnglishLevel);
                writer.WriteElementString("ExperienceNumber", lstEnrolls[i].ExperienceNumber.ToString());
                writer.WriteElementString("Quantity", lstEnrolls[i].Quantity.ToString());
                writer.WriteElementString("Salary", lstEnrolls[i].Salary);
                writer.WriteElementString("MajorName", major.MajorName);
                writer.WriteElementString("IsShow", lstEnrolls[i].IsShow.ToString());
                writer.WriteElementString("CreatedDate", lstEnrolls[i].CreatedDate.ToString());
                writer.WriteElementString("EndDate", lstEnrolls[i].EndDate.ToString());
                writer.WriteElementString("Area", lstEnrolls[i].Area);
                writer.WriteElementString("Lat", lstEnrolls[i].Lat);
                writer.WriteElementString("Lng", lstEnrolls[i].Lng);
                writer.WriteElementString("LeaderName", lstEnrolls[i].LeaderName);
                writer.WriteElementString("LeaderPhone", lstEnrolls[i].LeaderPhone);
                writer.WriteElementString("LeaderEmail", lstEnrolls[i].LeaderEmail);
                writer.WriteEndElement();
            }
        }

        // Close all tags
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();

        return stm;
    }

    public static List<ENROLLINFORMATION> GetListEnrollsWithMajorID(int majorID)
    {
        FindJobDBDataContext db = new FindJobDBDataContext();

        List<ENROLLINFORMATION> result = new List<ENROLLINFORMATION>();

        var query = from n in db.ENROLLINFORMATIONs
                    where n.MajorID == majorID
                    select n;

        foreach (ENROLLINFORMATION r in query)
            result.Add(r);

        return result;
    }

#endregion
        
    }
}
